package com.ssyu.examination.system.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.entity.SysMenuEntity;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.system.entity.CourseEntity;
import com.ssyu.examination.system.entity.SystemEntiy;

@WebServlet("/CourseServlet")
public class CourseServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
       
    public CourseServlet() {
        super();
    }

    @Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if("listCourse".equals(command)){
			listCourse(request,response);
		} else if ("addCourse".equals(command)) {
			addCourse(request,response);
		} else if ("deleteCourse".equals(command)) {
			deleteCourse(request,response);
		} else if ("updateCourse".equals(command)) {
			updateCourse(request,response);
		} else if ("getCourse".equals(command)) {
			getCourse(request,response);
		} else if ("getFatherMenu".equals(command)) {
			getFatherMenu(request,response);
		} else if ("checkCourseName".equals(command)) {
			checkCourseName(request,response);
		}
    
    }

    /**
     * 课程名字唯一性验证
     * @param request
     * @param response
     */
    private void checkCourseName(HttpServletRequest request,
			HttpServletResponse response) {
    	String courseName = request.getParameter("courseName");
    	CourseEntity courseEntity = DBUtil.getUniqueResult(CourseEntity.class, "SELECT * FROM sys_course WHERE text=?", courseName);

    	if (courseEntity != null) {
    		print(response, false);
    	} else {
    		print(response, true);
    	}		
	}

	/**
	 * 获取父级菜单
	 * @param request
	 * @param response
	 */
    private void getFatherMenu(HttpServletRequest request, HttpServletResponse response) {
    	int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		if (id == 0) {
			id = 1;
		}
		CourseEntity courseEntity = null;
		courseEntity = DBUtil
				.getUniqueResult(
						CourseEntity.class,
						"select id,text,pid from sys_course where id=?",
						id);
		int pid = courseEntity.getPid();
		if (pid == 0) {
			print(response, FastjsonUtil.toJson(courseEntity));
		}
		courseEntity = DBUtil.getUniqueResult(CourseEntity.class,
				"select * from sys_course where id=?", pid);
		print(response, FastjsonUtil.toJson(courseEntity));

	}
  

    /**
     * 获取课程信息
     * @param request
     * @param response
     */
	private void getCourse(HttpServletRequest request, HttpServletResponse response) {
		int id = 0;
		String Strid = request.getParameter("id");
		if (Strid != null) {
			id = Integer.parseInt(Strid);
		}
		CourseEntity courseEntity = null;

		courseEntity = DBUtil.getUniqueResult(CourseEntity.class,
				"SELECT * FROM sys_course WHERE id = ?", id);
		int pid = courseEntity.getPid();
		if (pid == 0) {
			courseEntity = DBUtil
					.getUniqueResult(
							CourseEntity.class,
							"SELECT s1.*,s2.text as ptext FROM sys_course AS s1,sys_course AS s2 WHERE s1.id=s2.id AND s2.id=?",
							id);
		} else {
			courseEntity = DBUtil
					.getUniqueResult(
							CourseEntity.class,
							"SELECT s1.*,s2.text as ptext FROM sys_course AS s1,sys_course AS s2 WHERE s1.pid=s2.id AND s1.id=?",
							id);
		}
		print(response, FastjsonUtil.toJson(courseEntity));
	}

	/**
     * 新增课程信息
     * @param request
     * @param response
     */
	private void addCourse(HttpServletRequest request,
			HttpServletResponse response) {
		int pid = Integer.parseInt(request.getParameter("id"));
		String text = request.getParameter("courseName");
		
		int operateId =App.getOperateId(request, response);
		
		
		int row = DBUtil.executeUpdate(
				"INSERT INTO sys_course VALUES(NULL, ?, ?, ?,NOW(), 1,NULL)",
				pid,text,operateId);
		if(row == 1){
			print(response,1);
		}
	}

	/**
	 * 删除课程信息
	 * @param request
	 * @param response
	 */
	private void deleteCourse(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		int row = DBUtil.executeUpdate("DELETE FROM sys_course WHERE id=?", id);
		
		if(row == 1){
			print(response, 1);
		}
	}

	/**
	 * 修改课程信息
	 * @param request
	 * @param response
	 */
	private void updateCourse(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String text = request.getParameter("courseName");
		int row = DBUtil
				.executeUpdate(
						"UPDATE sys_course SET text=? WHERE id=?",
						text, id);

		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 列举课程信息
	 * @param request
	 * @param response
	 */
	private void listCourse(HttpServletRequest request,
			HttpServletResponse response) {
		int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}

		List<CourseEntity> courseList = DBUtil
				.executeQuery(
						CourseEntity.class,
						"SELECT sc.*,su.name AS 'user_name' FROM sys_course sc,sys_user su WHERE sc.operate_user_id=su.id AND pid=?",
						id);
		print(response, FastjsonUtil.toJsonWithDateFormat(courseList,
				"yyyy-MM-dd HH:mm:ss"));
	}
}
